package cn.com.duiba.millionaire.center.spider.spdierjob;

import cn.com.duiba.millionaire.center.spider.bean.YYBAppJsonBean;
import cn.com.duiba.millionaire.center.spider.bean.YYBJsonDataBean;
import cn.com.duiba.millionaire.center.spider.common.PageConsumer;
import cn.com.duiba.millionaire.center.spider.entity.SpiderAppInfoEntity;
import cn.com.duiba.millionaire.center.spider.service.SpiderAppInfoService;
import cn.com.duiba.wolf.utils.DateUtils;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 应用宝app数据
 * <p>
 * Created by sunchangji on 2018/6/6.
 */
@Component
public class YingyongbaoSpider {

    //app详情页面介绍
    private static final String APP_DETAIL_URL = "http://sj.qq.com/myapp/detail.htm?apkName=";

    private static final String SUB_STR = "pageContext=";

    @Autowired
    private SpiderAppInfoService spiderAppInfoService;

    public PageConsumer pageConsumer = page -> {

        String json = page.getJson().get();
        if (StringUtils.isNotEmpty(json)) {
            try {
                YYBJsonDataBean dataBean = JSONObject.parseObject(json, YYBJsonDataBean.class);
                List<YYBAppJsonBean> jsonBeans = dataBean.getObj();
                if (CollectionUtils.isNotEmpty(jsonBeans)) {
                    for (YYBAppJsonBean bean : jsonBeans) {
                        SpiderAppInfoEntity entity = new SpiderAppInfoEntity();
                        entity.setEmail(APP_DETAIL_URL + bean.getPkgName());
                        entity.setDescription(bean.getEditorIntro());
                        entity.setUpdatedDate(DateUtils.getDayStr(bean.getApkPublishTime() * 1000));
                        entity.setCompany(bean.getAuthorName());
                        entity.setCategory(bean.getCategoryName());
                        entity.setAppName(bean.getAppName());
                        spiderAppInfoService.inserAppInfo(entity);
                    }
                    String url = page.getUrl().get();
                    if (StringUtils.isNotEmpty(url)) {
                        page.addTargetRequest(getTargetUrl(url));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };


    public String getTargetUrl(String targetUrl) {
        String pageIndex = StringUtils.substringAfterLast(targetUrl, "pageContext=");
        String preUrl = targetUrl.substring(0, targetUrl.lastIndexOf(SUB_STR) + SUB_STR.length());
        return preUrl + (Long.valueOf(pageIndex) + 1);

    }
}
